<?php
namespace Home\Controller;
use Think\Controller;
//header("content-type:text/html;charset=utf-8");
//微信授权类
class BaseController extends Controller {
     protected $user_id;
     protected $openid;
     protected $nickname;
     protected $headimgurl;

 	 protected function _initialize(){
 	 	$this->user_id = session('user_id');
	 	$this->nickname = session('nickname');
	 	$this->openid = session('openid');
	 	$this->headimgurl = session('headimgurl');
	 	//var_dump($_SESSION['openid']);
 	    //$this->user_id = 1;
        //=========步骤1：网页授权获取用户openid============
        //通过code获得openid
        //unset($_SESSION['openid']);
	    if(!$_SESSION['openid']) {
	    	//var_dump(1);
	    	$requestUri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; #查询(query)的字符串。
			vendor('Weixinpay.WxPayPubHelper');
			//使用jsapi接口
        	$jsApi = new \JsApi_pub();
            /*if (!isset($_REQUEST['code'])){
            	//var_dump(2);
                //触发微信返回code码
                $url = $jsApi->createOauthUrlForCode($requestUri);
                //file_put_contents('456.txt', $url);
                //var_dump($url);
                //Header("Location: $url"); 
                header("Location:$url");
                exit;
            }else{
            	//var_dump(3);
                //获取code码，以获取openid
                  $code = $_GET['code'];
                  $jsApi->setCode($code);
                  $openid = $jsApi->getOpenId();
                   //获取微信用户信息 @author  phcai
                  $user_info = $jsApi->getUserInfo();
                  //获取微信用户信息 @author  wei
                  // $AccessToken = $jsApi->getAccessToken();
                  // $user_info = $jsApi->getUserData($AccessToken['access_token']);
                  //注册微信用户
                  self::userDeal($user_info);
              }*/
        }
        //var_dump($_SESSION);
	} 
	
	//注册微信用户
	private  function userDeal($user_info){
		//var_dump(4);
		if($user_info['openid']){
			$user = M('users')->where("openid='%s'",array($user_info['openid']))->count();
			if(!$user){
				$data['openid'] = $user_info['openid'];
				$data['nickname'] = $user_info['nickname'];
				$data['sex'] = $user_info['sex'];
				$data['city'] = $user_info['city'];
				$data['province'] = $user_info['province'];
				$data['country'] = $user_info['country'];
				$data['headimgurl'] = $user_info['headimgurl'];
				$data['last_ip'] = getIP();
				$data['last_login'] = time();
				$data['reg_time'] = time();
				$data['oauth'] = 'weixin';
				$result = M('users')->add($data);
			}else{
				//更新用户
				self::updateUser($user_info['openid']);
			}
			//var_dump(5);
			$user_data = M('users')->where("openid='%s'",array($user_info['openid']))->field('id, nickname, openid, headimgurl')->find();
			session('user_id', $user_data['id']);
			session('nickname', $user_data['nickname']);
			session('openid', $user_data['openid']);
			session('headimgurl', $user_data['headimgurl']);
			$this->user_id = session('user_id');
	 		$this->nickname = session('nickname');
	 		$this->openid = session('openid');
	 		$this->headimgurl = session('headimgurl');
		}
	}

	//更新用户
	public function updateUser($openid){
		$data = array(
			'last_login' => time(),
			'last_ip' => getIP()
		);
		$resutl = M('users')->where("openid='%s'",array($openid))->save($data);
		if($resutl){
			return true;
		}else{
			exit('更新用户失败');
		}
	}
	
	public function _empty($name){
	    //die("请求的方法{$name}无效！");
	    $this->assign('name', "请求的方法{$name}无效！");
	    $this->display('./Public/Error/404.html');
	}

	/**
	 * 轮播图
	 */
	public function benner(){
	    $field = array('id', 'link', 'advert_img');
	    $benner_data = M('advert')->field($field)->where("media_type = 1 and enabled = 1")->order('sort asc, id desc')->select();
	    $this->assign('benner', $benner_data);
	}
	
}

?>